/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.utils;

#if flash9
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
#end

/**
    Just a collection of useful functions (mostly xml).
**/
class Utils {

    public inline static function xmlHasElement(xml : Xml, name : String) : Bool {
        return xml.elementsNamed(name).hasNext();
    }
    
    public inline static function getXmlElement(xml : Xml, name : String) : Xml {
        return xml.elementsNamed(name).next();
    }
    
    public inline static function docOver(xml : Xml) : Xml {
        var doc = Xml.createDocument();
        doc.addChild(xml);
        return doc;
    }
    
    /**
        Blends the two colors together.
        If t = 0, then the resulting color is just color1. If t = 1, then it's 
        color2.
    **/
    public static function mixColors(color1 : Int, color2 : Int, t : Float) 
            : Int {
        var rgb1 = convertIntToRGB(color1);
        var rgb2 = convertIntToRGB(color2);
        
        return (rgb1.rb + Std.int((rgb2.rb - rgb1.rb) * t)) << 16 | 
               (rgb1.gb + Std.int((rgb2.gb - rgb1.gb) * t)) << 8 | 
               (rgb1.bb + Std.int((rgb2.bb - rgb1.bb) * t));
    }
    
    public static function convertIntToRGB(color : Int) : RGB {
        return { rb : color >> 16, gb : (color >> 8) & 0xFF, 
                 bb : color & 0xFF };
    }
    
    #if flash9
    public static function createGenericTextBox(text : String, color = 0xFFFFFF,
                                                size = 8) : TextField {
        var box = new TextField();
        
        box.width = 100;
        box.height = 30;
        box.selectable = false;
        box.textColor = color;
        box.text = text;
        
        var format = new TextFormat();
        format.align = TextFormatAlign.CENTER;
        format.size = size;
        format.font = "arial";
        box.setTextFormat(format);
        return box;
    }
    #end
}

typedef RGB = {
    var rb : Int;
    var gb : Int;
    var bb : Int;
};

#if unit

class UtilsTest extends haxe.unit.TestCase {
    
    var xml : Xml;
    
    public function new(){
		super();		
	}
    
    override public function setup() {
        xml = Xml.parse("<test><child/></test>").firstElement();
    }
    
	public function testXmlHasElement() {
		assertTrue(Utils.xmlHasElement(xml, "child"));
        assertFalse(Utils.xmlHasElement(xml, "blah"));
	}
    
    public function testGetXmlElement() {
        assertEquals("child", Utils.getXmlElement(xml, "child").nodeName);
    }
    
    public function testDocOver() {
        var x2 = Utils.docOver(xml);
        assertEquals("test", x2.firstElement().nodeName);
        assertEquals(Xml.Document, x2.nodeType);
    }
}
#end